﻿package com.videointeractive.tool
{
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.Shape;
	
	import flash.net.NetStream;
	
	import com.videointeractive.conf.VideoDesignConf;
	
	import com.videointeractive.manager.VideoManager;
	
	
	public class VideoTrackBar extends Sprite
	{
		private static const TRACKBAR_DEFAULT_COLOR_BACK 	: uint = 0x000000;
		private static const TRACKBAR_DEFAULT_COLOR_FRONT	: uint = 0xFFFFFF;
		
		private var _colors 						: Object =	{ 
																	back: 	TRACKBAR_DEFAULT_COLOR_BACK,
																	front:	TRACKBAR_DEFAULT_COLOR_FRONT
																};
																
		private var _targetVid						: VideoManager;
		
		private var _backBar						: Shape;
		private var _frontBar						: Shape;
		
		private var _maxWidth						: Number = VideoDesignConf.TRACKBAR_MAX_WIDTH;
		
		private var _posX							: Number = VideoDesignConf.TRACKBAR_DEFAULT_POSITION_X;
		private var _posY							: Number = VideoDesignConf.TRACKBAR_DEFAULT_POSITION_Y;
		
		private var _initialized					: Boolean = false;
		
		public function VideoTrackBar(
									  	pTargetVideo	: VideoManager,
									  	pBackColor 		: uint = TRACKBAR_DEFAULT_COLOR_BACK,
										pFrontColor		: uint = TRACKBAR_DEFAULT_COLOR_FRONT
									  )
		{
			target = pTargetVideo;
			
			colors.back = pBackColor;
			colors.front = pFrontColor;
			
			init( );
		}
		
		private function init ( ) : void
		{
			_backBar 	= new Shape( );
			_frontBar 	= new Shape( );
			
			_backBar.x 	= 0;
			_backBar.y	= 0;
			
			_frontBar.x = 0;
			_frontBar.y = 0;
			
			generateShape( _backBar, colors.back, _maxWidth );
			generateShape( _frontBar, colors.front, 0 );
			
			addChildAt( _backBar, 0 );
			addChildAt( _frontBar, 1 );
			
			trigger_updateFrontShape( );
			
			_initialized = true;
		}
		
		private function generateShape( pShape : Shape, pColor : uint, pWidth : Number ) : void
		{
			pShape.graphics.clear();
			pShape.graphics.beginFill( pColor );
			pShape.graphics.drawRect( pShape.x, pShape.y, pWidth, VideoDesignConf.TRACKBAR_DEFAULT_HEIGHT );
			pShape.graphics.endFill();
		}
		
		public function trigger_updateFrontShape( ) : void
		{			
			if ( ! hasEventListener( Event.ENTER_FRAME ) )
				addEventListener( Event.ENTER_FRAME, updateFrontShape );
		}
		
		private function updateFrontShape( pEvt : Event ) : void
		{
			if ( ! target.isPlaying )
				removeEventListener( Event.ENTER_FRAME, updateFrontShape );
			else
			{
				var cWidth : Number = ( NetStream( target.stream ).time / target.duration ) * VideoDesignConf.TRACKBAR_MAX_WIDTH;
				cWidth = Math.round( cWidth );
				
				generateShape( _frontBar, colors.front, cWidth );
			}
		}
		
		public function get colors ( ) : Object
		{
			return _colors;
		}
		
		public function set colors ( val : Object ) : void
		{
			_colors = val;
		}
		
		public function get target ( ) : VideoManager
		{
			return _targetVid;
		}
		
		public function set target ( val : VideoManager ) : void
		{
			_targetVid = val;
		}
	}
}